import sun.tracing.dtrace.DTraceProviderFactory;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-05-18
 * Time: 12:00
 */
public class MyStack {
    public int[] elem;
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 10;
    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }
    /**
     * 入栈
     */
    public void push(int val){
        if(isFull()){
            elem = Arrays.copyOf(elem , elem.length*2);
        }
        elem[usedSize] = val;
        usedSize++;
    }
    /**
     * 出栈
     */
    public int pop(){
      if(isEmpty()){

          throw new NullPointerException("栈为空!");
      }
      int oldVal = elem[usedSize-1];
      usedSize--;
      return oldVal;
    }
    /**
     * 查看栈顶元素
     */
    public int peek(){
        if(isEmpty()){

            throw new NullPointerException("栈为空!");
        }
        return elem[usedSize-1];
    }
    /**
     * 判空
     */
    public boolean isEmpty(){
        return usedSize == 0;
    }
    /**
     * 判满
     */
    public boolean isFull(){
        return usedSize == elem.length ;
    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch=='[' || ch=='{' ){
                stack.push(ch);
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                //ch 是右括号
                char ch2 = stack.pop();
              if(ch2 == '(' && ch == ')' || ch2 == '[' && ch == ']' ||  ch2 == '{' && ch == '}' ){
                  return true;
              }
            }

        }
       return stack.isEmpty();

    }
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String str = tokens[i];
            if(str.equals("+")|| str.equals("-")|| str.equals("*")|| str.equals("/")){
                int num1 = 0;
                int num2=0;
                if(!stack.isEmpty()){
                    num2 = stack.pop();
                }else{
                    return -1;
                }
                if(!stack.isEmpty()){
                    num1 = stack.pop();
                }else {
                    return -1;
                }

                switch (str){
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-" :
                        stack.push(num1-num2);
                        break;
                    case "*":
                        stack.push(num1*num2);
                        break;
                    case "/":
                        stack.push(num1/num2);
                        break;
                }

            }else{
                int n = Integer.valueOf(str);
                stack.push(n);
            }
        }
        return stack.pop();

    }
}
